package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.basedata.dao.BarcodeMachineDao;
import com.source.lightfowl.basedata.dao.BarcodeMachineMsgDao;
import com.source.lightfowl.basedata.dao.BarcodeMachineStatusDao;
import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.basedata.entity.BarcodeMachineMsg;
import com.source.lightfowl.basedata.service.BarcodeMachineMsgService;
import com.source.lightfowl.constant.ImageCatalogue;
import com.source.lightfowl.utils.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: xcy
 * @Date:Create：in 2020/9/27 11:04
 * @Description:
 */
@Component
public class BarcodeMachineMsgServiceImpl implements BarcodeMachineMsgService {

    @Autowired
    private BarcodeMachineMsgDao dao;
    @Autowired
    private BarcodeMachineStatusDao barcodeMachineStatusDao;
    @Autowired
    private BarcodeMachineDao barcodeMachineDao;

    @Override
    public List<BarcodeMachineMsg> findAll() {
        return dao.findAll();
    }

    @Override
    public Page<BarcodeMachineMsg> findAllPage(int page, int pageNum) {
        return dao.findAll(PageRequest.of(page-1, pageNum));
    }

    @Override
    public Optional<BarcodeMachineMsg> findById(String id) {

        return dao.findById(id);
    }

    @Transactional
    @Override
    public void save(BarcodeMachineMsg sh, MultipartFile file, HttpServletRequest request) throws Exception {
        //to do 3.添加和修改
        //更新图片
        if (file != null){
            String s = null;

            s = ImageUtil.saveImage(file, ImageCatalogue.BarcodeMachine_Catalogue);

            //删除原来的图片
            String mag = sh.getMag();
            if(StringUtils.isEmpty(mag)){
                //新增 不用处理
            }{
                //修改 则删除原来的图片
                ImageUtil.delete(mag,ImageCatalogue.BarcodeMachine_Catalogue);
            }
            sh.setMag(s);
        }

        dao.save(sh);
    }

    @Override
    public void deleteById(String id) {
        //1.先删除服务器存储的图片
        Optional<BarcodeMachineMsg> optional = dao.findById(id);
        if (optional.isPresent()){
            BarcodeMachineMsg machineMsg = optional.get();
            String mag = machineMsg.getMag();
            ImageUtil.delete(mag,ImageCatalogue.BarcodeMachine_Catalogue);
        }
        //2.删除数据
        //dao.deleteById(id);
        cascadingDeleteById(id);//级联删除
    }

    @Override
    public void deleteBatch(String ids) {
        String[] strings = ids.split(",");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i <strings.length ; i++) {
            list.add(strings[i]);
        }
        dao.deleteBatch(list);
    }

    @Override
    public Long getCount() {
        return dao.count();

    }

    @Override
    public void deleteImage(String ids) {
        //修改数据库


        //删除图片
    }

    @Override
    public void cascadingDeleteById(String id) {

        //删除 三个表的(关联 所有 已经出厂) 脚标机记录
        /*
        直接删除 不管是否存在这条记录 如果不存在 会抛出异常 所以都要catch
         */
        //1.所有
        try {
            //
            dao.deleteById(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        //2.出厂
        try {
            //
            barcodeMachineDao.deleteById(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        //3.关联信息表
        try {
            //
            //barcodeMachineStatusDao.deleteById(id);
            barcodeMachineStatusDao.deleteByBarchodeMachineId(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
